package com.tool.util;

import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @author liuxuhuang
 * @Product IntelliJ IDEA
 * @Package com.tool.util
 * @Title create
 * @Description: 产生随机字符串工具类
 * @email liuxuhuang3970@qq.com
 * @date 2021/10/25 16:44
 */
@Component
public class Create {
    /**
     * 产生数字字符串
     *
     * @param length
     * @return
     */
    public String createNumber(int length) {
        String str = "0123456789";
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(9);
            stringBuffer.append(str.charAt(number));
        }
        return String.valueOf(stringBuffer);
    }

    /**
     * 产生字母字符串
     *
     * @param length
     * @return
     */
    public String createString(int length) {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        Random random = new Random();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(52);
            stringBuffer.append(str.charAt(number));
        }
        return String.valueOf(stringBuffer);
    }

    /**
     * 产生随机字符串
     *
     * @param length
     * @return
     */
    public String createStringWithNumber(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if ("char".equalsIgnoreCase(charOrNum)) {
                int tmp = random.nextInt(2) % 2 == 0 ? 64 : 97;
                val += (char) (random.nextInt(26) + tmp);
            } else if ("num".equalsIgnoreCase(charOrNum)) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }
}
